<?php

/**
 * 73YC 名品购
 * ============================================================================
 * * 版权所有 20013-* 梅州络客网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.alork.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2013-10-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

//判断是否有ajax请求
$act = !empty($_GET['act']) ? $_GET['act'] : '';
if ($act == 'cat_rec')
{
    $rec_array = array(1 => 'best', 2 => 'new', 3 => 'hot');
    $rec_type = !empty($_REQUEST['rec_type']) ? intval($_REQUEST['rec_type']) : '1';
    $cat_id = !empty($_REQUEST['cid']) ? intval($_REQUEST['cid']) : '0';
    include_once('includes/cls_json.php');
    $json = new JSON;
    $result   = array('error' => 0, 'content' => '', 'type' => $rec_type, 'cat_id' => $cat_id);

    $children = get_children($cat_id);
    $smarty->assign($rec_array[$rec_type] . '_goods',      get_category_recommend_goods($rec_array[$rec_type], $children));    // 推荐商品
    $smarty->assign('cat_rec_sign', 1);
    $result['content'] = $smarty->fetch('library/recommend_' . $rec_array[$rec_type] . '.lbi');
    die($json->encode($result));
}

/*------------------------------------------------------ */
//-- 判断是否存在缓存，如果存在则调用缓存，反之读取相应内容
/*------------------------------------------------------ */
/* 缓存编号 */
$cache_id = sprintf('%X', crc32($_SESSION['user_rank'] . '-' . $_CFG['lang']));

if (!$smarty->is_cached('indexxxx.dwt', $cache_id))
{
    /* 获得请求的分类 ID */
    if (isset($_REQUEST['id'])){
        $cat_id = intval($_REQUEST['id']);
    }
    else{
        $cat_id = 197;
    }
    assign_template();
    $position = assign_ur_here();
    $smarty->assign('cid',             $cat_id);
    $smarty->assign('page_title',      $position['title']);    // 页面标题
    $smarty->assign('keywords',        htmlspecialchars($_CFG['shop_keywords']));
    $smarty->assign('description',     htmlspecialchars($_CFG['shop_desc']));
    if($cat_id==197){
        $goods_list = category_get_goods(get_children($cat_id), "0", "0", "0", "", "10000", "1", "sort_order","ASC");
    }else{
        $goods_list = category_get_goods(get_children($cat_id), "0", "0", "0", "", "10000", "1", "goods_id","ASC");
    }
    $goods_list = array_values($goods_list);
    $list[0][]='';
    foreach($goods_list AS $key=>$value){
        $list[($key+1)%4][]=$value;
    }
    $goods_list = $list;
    unset($list);
    if(!$goods_list) header('location:index.php');
    $smarty->assign('goods_list',        $goods_list);
    $links = index_get_links();
    $navigator = get_navigator($ctype, $catlist);
    foreach($navigator['bottom'] AS $key=>$value){
        if(strpos($value['url'],"$cat_id")!==false){
            $navigator['bottom'][$key]['hover'] = 1;
        }
    }
    
    $smarty->assign('navigator', $navigator);
    $smarty->assign('img_links',       $links['img']);
    $smarty->assign('txt_links',       $links['txt']);
    $smarty->assign('data_dir',        DATA_DIR);       // 数据目录

}
$smarty->display('shopall.dwt', $cache_id);

/*------------------------------------------------------ */
//-- PRIVATE FUNCTIONS
/*------------------------------------------------------ */

/**
 * 获得所有的友情链接
 *
 * @access  private
 * @return  array
 */
function index_get_links()
{
    $sql = 'SELECT link_logo, link_name, link_url FROM ' . $GLOBALS['ecs']->table('friend_link') . ' ORDER BY show_order';
    $res = $GLOBALS['db']->getAll($sql);

    $links['img'] = $links['txt'] = array();

    foreach ($res AS $row)
    {
        if (!empty($row['link_logo']))
        {
            $links['img'][] = array('name' => $row['link_name'],
                                    'url'  => $row['link_url'],
                                    'logo' => $row['link_logo']);
        }
        else
        {
            $links['txt'][] = array('name' => $row['link_name'],
                                    'url'  => $row['link_url']);
        }
    }

    return $links;
}

//产品列表
function category_get_goods($children, $brand, $min, $max, $ext, $size, $page, $sort, $order)
{
    $display = $GLOBALS['display'];
    $where = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
            "g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';
    
    if ($brand > 0)
    {
        $where .=  "AND g.brand_id=$brand ";
    }

    if ($min > 0)
    {
        $where .= " AND g.shop_price >= $min ";
    }

    if ($max > 0)
    {
        $where .= " AND g.shop_price <= $max ";
    }
    /* 获得商品列表 */
    $sql = 'SELECT g.goods_id, g.goods_name, g.shop_price, g.shopall_note, g.goods_name_style, g.goods_type, g.goods_img, g.promote_price, g.promote_start_date, g.promote_end_date, gg.thumb_url AS goods_thumb ' . 
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' . 
            'LEFT JOIN ' . $GLOBALS['ecs']->table('goods_gallery') . ' AS gg ' . 
                "ON gg.goods_id = g.goods_id AND img_desc = 6 " . 
            "WHERE $where $ext ORDER BY $sort $order,goods_id DESC";

    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $promote_price = 0;
        }

        $arr[$row['goods_id']]['goods_id']           = $row['goods_id'];
        if($display == 'grid')
        {
            $arr[$row['goods_id']]['goods_name']     = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        }
        else
        {
            $arr[$row['goods_id']]['goods_name']     = $row['goods_name'];
        }
        $arr[$row['goods_id']]['name']               = preg_split('/[\r\n]+/',$row['shopall_note']);
        $arr[$row['goods_id']]['goods_style_name']   = add_style($row['goods_name'],$row['goods_name_style']);
        $arr[$row['goods_id']]['shop_price']         = format_price($row['shop_price']);
        if($promote_price){
            $arr[$row['goods_id']]['promote_price']  = format_price($row['promote_price']);
        }else{
            $arr[$row['goods_id']]['promote_price']  = '';
        }
        $arr[$row['goods_id']]['type']               = $row['goods_type'];
        $arr[$row['goods_id']]['goods_thumb']        = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']          = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']                = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
    }
    return $arr;
}
?>